// 自定义一系列弹窗

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:love_app/common/su.dart';

class SuDialog {
  // ios风格对话框
  Future<void> errorDialog(BuildContext context, String title,
      {Object content}) async {
    String _content = content == null ? '' : content.toString();
    await showDialog(
      //通过showDialog方法展示alert弹框
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text(title), //弹框标题
          content: Text(_content), //弹框内容
          actions: <Widget>[
            //操作控件
            FlatButton(
              onPressed: () {
                //控件点击监听
                Navigator.pop(context);
              }, //按钮上的文本风格
              child: Text('取消'), //控件显示内容
            ),
            FlatButton(
              onPressed: () {
                Navigator.pop(context);
              },
              child: Text('确定'),
            ),
          ],
        );
      },
    );
  }

  // 轻量级提示框
  static showToast(String msg,
      {Color bgColor, Color textColor, double textSize, Toast toastLength}) {
    Fluttertoast.showToast(
      msg: msg,
      toastLength: toastLength ?? Toast.LENGTH_SHORT,
      gravity: ToastGravity.CENTER,
      timeInSecForIos: 1,
      backgroundColor: bgColor ?? Colors.pink,
      textColor: textColor ?? Colors.white,
      fontSize: Su().s(25),
    );
  }

  // 异步widget等待是显示
  static Widget futureBuildLoding({Color textColor}) {
    return Container(
      height: Su().h(1000),
      width: Su().w(750),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          CupertinoActivityIndicator(
            radius: 40.0, //值越大加载的图形越大
          ),
          Container(
            padding: EdgeInsets.only(
              top: Su().h(30),
            ),
            child: Text(
              "拼命加载中...",
              style: TextStyle(
                color: textColor ?? Colors.black
              ),
            ),
          )
        ],
      ),
    );
  }
}

/// 自定义异步显示loading
class SuShowLoading {
  BuildContext context;
  String title;
  TextStyle textStyle;
  Color bgColor;
  Color loadColor;
  bool isCricle; // 显示框是否为圆形，默认为圆

  SuShowLoading(this.context,
      {this.title,
      this.textStyle,
      this.bgColor,
      this.loadColor,
      this.isCricle});

  /// 创建异步loading
  Future buildLoading() async {
    await showDialog(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return _showLoading();
      },
    );
  }

  /// loading样式
  Widget _showLoading() {
    TextStyle _textStyle = textStyle ??
        TextStyle(
          color: Colors.blue,
          fontSize: Su().s(30),
        );
    Color _loadColor = loadColor ?? Colors.black;
    bool _isCircle = isCricle ?? true;
    return Center(
      child: SizedBox(
        width: 120.0,
        height: 120.0,
        child: Container(
          decoration: BoxDecoration(
            color: bgColor ?? Colors.white, // 背景颜色
            borderRadius: BorderRadius.circular(_isCircle == true ? 90 : 10),
          ),
          child: _showLoadingContainerChild(_loadColor, _textStyle),
        ),
      ),
    );
  }

  /// loading显示动画样式
  Widget _showLoadingContainerChild(Color _loadColor, TextStyle style) {
    String _title = title ?? '请稍等...';
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        CircularProgressIndicator(
          // 如果不需要自定义动画风格，就直接传AlwaysStoppedAnimation就好了。
          valueColor: AlwaysStoppedAnimation(_loadColor),
        ), // 显示沿圆圈进度的小部件（循环）
        Padding(
          padding: EdgeInsets.only(
            top: Su().h(20),
          ),
          child: Text(
            _title,
            style: style,
          ),
        ),
      ],
    );
  }

  // 取消loading
  SuShowLoading.loadingClose(BuildContext context) {
    this.context = context;
    Navigator.of(context).pop();
  }
}
